<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ServiceGroups.class.php, $Author:  $ */



/** Service for working with groups
 *
 * @package module.groups
 * @version $Id: PhpFox_ServiceGroups.class.php 445 2008-04-28 06:41:30Z phpfoxsv $
 */
class PhpFox_Mod_Groups_ServiceGroups extends BaseService
{
    /**
     * CALLBACK when event 'delete accout' accuried
     *
     * @param array $aParams array of member id
     */
    function onDeleteAccount($aParams)
    {
        $oGalleryService = $this->_oModule->getService('Gallery');
        $oChatService = &App::getModuleService('Shoutbox', 'Shoutbox');

        $aIds = $aParams['aIds'];

        $oAccountService = &App::getModuleService('Account', 'Account');

        foreach($aIds as $iUserId)
        {
            $oAcc = $oAccountService->getById($iUserId);
	        $oItem = $this->_oModule->getItem('Groups');

	        //clear group members
	        $sSql = 'DELETE FROM '.App::getT('group_member').' WHERE `user`="'.$oAcc->aData['user'].'"';
	        $oItem->oDb->query($sSql);

	        //clear invite data
	        $sSql = 'DELETE FROM '.App::getT('group_invite').' WHERE `user`="'.$oAcc->aData['user'].'" OR `invite`="'.$oAcc->aData['user'].'"';
	        $oItem->oDb->query($sSql);

	        //clear shoutbox
	        //load user groups
	        $sSql = 'SELECT gm.id FROM '.App::getT('group_main').' gm WHERE gm.user="'.$oAcc->aData['user'].'"';
	        $aGrp = $oItem->oDb->getRows($sSql);
	        if($aGrp)
	        {
	            $aG = array();
                foreach($aGrp as $v)
                    $aG[] = $v['id'];

    	        $sSql = 'DELETE FROM '.App::getT('chat').' WHERE `group_id` IN ("'.implode('","',$aG).'")';
    	        $oItem->oDb->query($sSql);
	        }

	        //clear gallery photos
	        //load items
	        $sSql = 'SELECT id FROM '.App::getT('group_gallery').' WHERE `user`="'.$oAcc->aData['user'].'"';
	        $aImages = $oItem->oDb->getRows($sSql);
	        foreach($aImages as $aId)
	        {
	            $oGalleryService->deleteItem($aId['id']);
	        }

	        //clear user's forum replies
	        $sSql = 'DELETE FROM '.App::getT('group_forum').' WHERE `user`="'.$oAcc->aData['user'].'" AND id <> reply_id';
	        $oItem->oDb->query($sSql);

	        //clear forum topics and replies
	        //load topics
	        $sSql = 'SELECT id FROM '.App::getT('group_forum').' WHERE `user`="'.$oAcc->aData['user'].'" AND `id`=`reply_id`';
	        $aTopics = $oItem->oDb->getRows($sSql);
	        foreach($aTopics as $aId)
	        {
	            //clear posts
	            $sSql = 'DELETE FROM '.App::getT('group_forum').' WHERE `reply_id`="'.$aId['id'].'"';
	            $oItem->oDb->query($sSql);
	        }

	        //delete user's groups
	        $sSql = 'SELECT gm.id FROM '.App::getT('group_main').' gm WHERE gm.user="'.$oAcc->aData['user'].'"';
	        $aGrp = $oItem->oDb->getRows($sSql);
	        foreach($aGrp as $aId)
	        {
	            //clear forum
	            $sSql = 'DELETE FROM '.App::getT('group_forum').' WHERE `group_id`="'.$aId['id'].'"';
    	        $oItem->oDb->query($sSql);

    	        //clear gallery photos
		        //load items
		        $sSql = 'SELECT id FROM '.App::getT('group_gallery').' WHERE `group_id`="'.$aId['id'].'"';
		        $aImages = $oItem->oDb->getRows($sSql);
		        foreach($aImages as $imgId)
		        {
		            $oGalleryService->deleteItem($imgId['id']);
		        }

	            //clear members
	            $sSql = 'DELETE FROM '.App::getT('group_member').' WHERE `group_id`="'.$aId['id'].'"';
    	        $oItem->oDb->query($sSql);

    	        //clear invites
    	        $sSql = 'DELETE FROM '.App::getT('group_invite').' WHERE `group_id`="'.$aId['id'].'"';
    	        $oItem->oDb->query($sSql);

    	        //clear invites
    	        $sSql = 'DELETE FROM '.App::getT('group_main').' WHERE `id`="'.$aId['id'].'"';
    	        $oItem->oDb->query($sSql);
	        }

        }
    }

    function addGroupView($iGroupId)
    {
        $oGroupItem = $this->_oModule->getItem('Groups');
        /* @var $oGroupItem PhpFox_Mod_Groups_ItemGroups */

        $sSql ='UPDATE `'.App::getT('group_main').'` SET `views` = `views` + 1 WHERE `id`="'.intval($iGroupId).'"';
        $oGroupItem->oDb->query($sSql);
    }

    /**
     * Load group categories
     *
     * @return array category list
     */
    function getCategories()
    {
        $oGroupItem = $this->_oModule->getItem('Category');
        /* @var $oGroupItem PhpFox_Mod_Groups_ItemCategory */

        $sSql = 'SELECT c.id, c.name, c.total cnt
				 FROM '.App::getT('group_name').' c
				 ORDER BY c.name ASC';
        $aResult = $oGroupItem->oDb->getRows($sSql);
        foreach ($aResult as $iKey => $aRow)
        {
        	$aResult[$iKey]['name'] = App::formatTranslate($aRow['name']);
        }

        $this->Category = $aResult;

        return $aResult;
    }

    /**
     * Load category data
     *
     * @return number category_id
     */
    function getCategory($iCategoryId)
    {
        $oGroupItem = $this->_oModule->getItem('Category');
        /* @var $oGroupItem PhpFox_Mod_Groups_ItemCategory */

        $sSql = 'SELECT c.id, c.name
				 FROM '.App::getT('group_name').' c
			     WHERE c.id='.intval($iCategoryId);
        $aResult = $oGroupItem->oDb->getRow($sSql);
        return $aResult;
    }

    /**
     * Load new categories
     *
     * @return array category list
     */
    function getNewGroups()
    {
        $sSql ='SELECT gm.id, gm.hide_group, gm.location, gm.title, gm.user, gm.time, gm.headline, gg.id as galid, gg.img, IF(gmem.user != "", 1, 0) AS is_user
				FROM '.App::getT('group_main').' gm
				LEFT JOIN '.App::getT('group_gallery').' gg ON (gm.id=gg.group_id AND gg.`default`="1")
				LEFT JOIN ' . App::getT('group_member') . ' AS gmem ON(gm.id = gmem.group_id AND gmem.user = "' . App::getUser('user') . '")
				ORDER BY `time`
				DESC LIMIT 0, 5';

        $aResult = $this->oDatabase->getRows($sSql);
        return $aResult;
    }

    /**
     * Get list of users groups
     *
     * @param string $sUser
     * @param int $iPage
     * @param int $iPageSize
     * @return array of groups ant total count
     */
    function getMembership($sUser, $iPage = 0, $iPageSize = 0)
    {
        $oGroupItem = $this->_oModule->getItem('Groups');
        //calc total
  		$sSql = 'SELECT COUNT(DISTINCT(group_id)) AS cnt
				FROM `'.App::getT('group_member').'`
				WHERE `user`="'.$sUser.'"';
  		$aResultTotal = $oGroupItem->oDb->getRow($sSql);

  		$sSql = 'SELECT
				gmem.id as memid,
				gm.id,
				gm.title,
				gm.user,
				gg.id as galid
				FROM '.App::getT('group_main').' gm
				INNER JOIN '.App::getT('group_member').' gmem ON(gm.id=gmem.group_id)
				LEFT JOIN '.App::getT('group_gallery').' gg ON (gm.id=gg.group_id AND gg.`default`="1")
				WHERE gmem.user="'.$sUser.'"
				GROUP BY gm.id '.
				(($iPage || $iPageSize)?(' LIMIT '.($iPage - 1)*$iPageSize.','.$iPageSize):'');
		$aResult = $oGroupItem->oDb->getRows($sSql);
        return array($aResult, $aResultTotal['cnt']);
    }

    /**
     * Search groups by condition
     *
     * @param array $aCond array list of conditions
     * @param int $iPage page number
     * @param int $iPageSize page sizr
     * @param string $sOrder additional order
     * @return array groups and total count
     */

    function searchGroups($aCond, $iPage, $iPageSize, $sOrder = '', $sAddCondition='')
    {
  		$oGroupItem = $this->_oModule->getItem('Groups');
  		$sCond = '';
  		foreach($aCond as $sStr)
  		{
  			$sCond .= ' AND gm.'.str_replace(array('{','#','}'),'',$sStr);
  		}

  		$sSql = 'SELECT COUNT(gm.id) AS cnt
				FROM '.App::getT('group_main').' gm
				WHERE 1 '.$sCond.' '.$sAddCondition;

  		$aResultTotal = $oGroupItem->oDb->getRow($sSql);

		$sSql = 'SELECT gm.id, gm.hide_group, gm.location, gm.title, gm.user, gm.time, gm.headline, gg.id as galid, gg.img, count(gmem.id) AS cnt
				FROM '.App::getT('group_main').' gm
				LEFT JOIN '.App::getT('group_member').' gmem ON(gm.id=gmem.group_id)
				LEFT JOIN '.App::getT('group_gallery').' gg ON (gm.id=gg.group_id AND gg.`default`="1")
				WHERE 1 '.$sCond.' '.$sAddCondition.'
				GROUP BY gm.id '.
				(($sOrder == '')?'':' ORDER BY '.$sOrder).
				' LIMIT '.($iPage - 1)*$iPageSize.','.$iPageSize;
		$aResult = $oGroupItem->oDb->getRows($sSql);
        return array($aResult, $aResultTotal['cnt']);
    }

    /**
     * Get invite data
     *
     * @param int $iGroupId group id
     * @param string $sUser
     * @return array
     */

    function getInviteData($iGroupId, $sUser)
    {
        $oInviteItem = $this->_oModule->getItem('GroupInvite');
        $sSql = 'SELECT id, type_id, group_id, user, invite, time FROM '.App::getT('group_invite').'
			     WHERE group_id = '.intval($iGroupId).' AND invite="'.$oInviteItem->oDb->escape($sUser).'"';
        $aResult = $oInviteItem->oDb->getRow($sSql);
        return $aResult;
    }

    /**
     * Remove invite if user press `decline`
     *
     * @param int $iInviteId invite id
     */

    function removeInvite($iInviteId)
    {
        $oGroupInviteItem = $this->_oModule->getItem('GroupInvite');
		$sSql = 'DELETE FROM '.App::getT('group_invite').' WHERE id="'.intval($iInviteId).'"';
        $oGroupInviteItem->oDb->query($sSql);
    }

    /**
     * Add member to group
     *
     * @param int $iGroupId group id
     * @param int $sUser user
     * @param int $nTypeId group category
     * @param int $iInviteId invite id
     */

    function addGroupMember($iGroupId, $sUser, $nTypeId, $iInviteId = 0)
    {
        $oGroupMemberItem = $this->_oModule->getItem('GroupMember');

        $sSql = 'INSERT INTO '.App::getT('group_member').' (type_id, group_id, user, time) VALUES ('.
                 intval($nTypeId).','.intval($iGroupId).',"'.$oGroupMemberItem->oDb->escape($sUser).'",'.time().')';
        $oGroupMemberItem->oDb->query($sSql);

        if($iInviteId)
            $this->removeInvite($iInviteId);
    }


    /**
     * Check is user group member
     *
     * @param int $iGroupId
     * @param string $sUser
     * @param int $iRecordId additional check
     * @return int
     */

    function isMember($iGroupId, $sUser, $iRecordId = 0)
    {
        $iRecordId = intval($iRecordId);
        $iGroupId = intval($iGroupId);

        $oGroupMemberItem = $this->_oModule->getItem('GroupMember');
        $sSql = 'SELECT COUNT(id) AS cnt
				 FROM '.App::getT('group_member').'
				 WHERE user="'.$oGroupMemberItem->oDb->escape($sUser).'"
				 AND `group_id`='.$iGroupId;
        if($iRecordId)
            $sSql .= ' AND id="'.$iRecordId.'"';

        $aResult = $oGroupMemberItem->oDb->getRow($sSql);
        return intval($aResult['cnt']);
    }

    /**
     * Check is user group owner
     *
     * @param int $iGroupId
     * @param string $sUser
     * @return int
     */

    function isOwner($iGroupId, $sUser)
    {
        $oGroupItem = $this->_oModule->getItem('Groups');
        $sSql = 'SELECT COUNT(id) AS cnt FROM '.App::getT('group_main').' WHERE user="'.$oGroupItem->oDb->escape($sUser).'" AND `id`="'.$iGroupId.'"';
        $aResult = $oGroupItem->oDb->getRow($sSql);
        return intval($aResult['cnt']);
    }

    /**
     * Get group information
     *
     * @param int $iGroupId
     * @return array of data
     */

    function getGroupData($iGroupId)
    {
        $iGroupId = intval($iGroupId);
        $oGroupItem = $this->_oModule->getItem('Groups');
        $sSql =    'SELECT
						gc.id AS category_id,
						gc.name AS category_name,
						g.type,
						g.id,
						g.user,
						g.title,
						g.headline,
						g.text,
						g.email,
						g.hide_group,
						g.time,COUNT(gm.id) AS members,
						g.views,
						g.city,
						g.location,
						g.open_join,
						g.hide_group,
						g.mem_invite,
						g.pub_forum,
						g.mem_image,
						gg.id as galid,
						g.state,
						g.zip
					FROM '.App::getT('group_main').' g
					LEFT JOIN '.App::getT('group_name').' gc ON(g.type=gc.id)
					LEFT JOIN '.App::getT('group_member').' gm ON(g.id=gm.group_id)
					LEFT JOIN '.App::getT('group_gallery').' gg ON (g.id=gg.group_id AND gg.`default`="1")
					WHERE g.id = '.$iGroupId.'
					GROUP BY g.id';
        $aResult = $oGroupItem->oDb->getRow($sSql);
        return $aResult;
    }

    /**
     * Get group members
     *
     * @param int $iGroupId group id
     * @param int $iPage page number
     * @param int $iPageSize page size
     * @return array of total count and data
     */

    function getGroupMember($iGroupId, $iPage, $iPageSize)
    {
        $iGroupId = intval($iGroupId);
        $oGroupMemberItem = $this->_oModule->getItem('GroupMember');
        $sSql = 'SELECT COUNT(id) AS cnt FROM '.App::getT('group_member').' WHERE `group_id` = "'.$iGroupId.'"';
		$aResult = $oGroupMemberItem->oDb->getRow($sSql);
		$nTotalCnt = $aResult['cnt'];

		$sSql = 'SELECT gm.`id`, gm.`user`
				 FROM '.App::getT('group_member').' gm
				 WHERE `group_id` = '.$iGroupId.'
				 ORDER BY `time` DESC
                 LIMIT '.($iPage - 1)*$iPageSize.','.$iPageSize;
        $aResult = $oGroupMemberItem->oDb->getRows($sSql);
		return array($nTotalCnt, $aResult);
    }

    /**
     * Delete member from group
     *
     * @param int $iRecordId record if
     */

    function deleteFromGroup($iRecordId, $sOwner = '')
    {
        $iRecordId = intval($iRecordId);
        $oGroupMemberItem = $this->_oModule->getItem('GroupMember');
        $sSql = 'DELETE FROM `'.App::getT('group_member').'` WHERE id="'.$iRecordId.'"';
        if($sOwner)
            $sSql .= ' AND user<>"'.$oGroupMemberItem->oDb->escape($sOwner).'"';
        $oGroupMemberItem->oDb->query($sSql);
    }

	function updateGroupCount()
	{
		// Get all the group cats.
	 	$aRows = $this->oDatabase->getRows("SELECT id FROM ". App::getT('group_name') ."");
		foreach($aRows as $aRow)
		{
			// Get the total group count
			$iCnt = $this->oDatabase->getRow("SELECT COUNT(*) AS count FROM ". App::getT('group_main') ." WHERE type = '". $aRow['id'] ."'");
			// Update the group total count
			$this->oDatabase->query("UPDATE ". App::getT('group_name') ." SET total = '". $iCnt['count'] ."' WHERE id = '". $aRow['id'] ."'");
		}
	 }

    /**
     * Insert or update group
     *
     * @param int $iGroupId group id
     * @param array $aData array of data field=>value
     * @return int group id if new group
     */
    function updateGroupData($iGroupId, $aData)
    {
        $iGroupId = intval($iGroupId);
        $oGroupsItem = $this->_oModule->getItem('Groups');
        //update
        $sStr = '';
        foreach($aData as $key=>$val)
        {
            $sStr .= '`'.$key.'`="'.$oGroupsItem->oDb->escape($val).'",';
        }
        $sStr = substr($sStr, 0, -1);

        if($iGroupId)
        {
            $sSql = 'UPDATE '.App::getT('group_main').' SET '.$sStr.' WHERE `id`="'.$iGroupId.'"';
            $oGroupsItem->oDb->query($sSql);
        }
        //insert
        else
        {
            $sSql = 'INSERT INTO '.App::getT('group_main').' SET '.$sStr;
            $oGroupsItem->oDb->query($sSql);
            return $oGroupsItem->oDb->getLastID();
        }
    }

    /**
     * Remove group and invited members from list
     *
     * @param int $iGroupId
     * @param int $aFriends
     * @return array
     */
    function removeInvited($iGroupId, $aFriends)
    {
        $oGroupsItem = $this->_oModule->getItem('Groups');

        //load group members
        $sSql = 'SELECT user FROM '.App::getT('group_member').' WHERE group_id="'.$iGroupId.'"';
        $aGroupMembersTmp = $oGroupsItem->oDb->getRows($sSql);
        $aGroupMembers = array();
        foreach($aGroupMembersTmp as $aVal)
            $aGroupMembers[]= $aVal['user'];

        //load invited members
        $sSql = 'SELECT invite FROM '.App::getT('group_invite').' WHERE group_id="'.$iGroupId.'"';
        $aGroupInviteTmp = $oGroupsItem->oDb->getRows($sSql);
        $aGroupInvite = array();
        foreach($aGroupInviteTmp as $aVal)
            $aGroupInvite[]= $aVal['invite'];

        //remove dublicates
        foreach($aFriends as $nKey=>$aVal)
        {
            if(in_array($aVal['user'], $aGroupMembers) ||
               in_array($aVal['user'], $aGroupInvite))
               {
                   unset($aFriends[$nKey]);
               }
        }
        return $aFriends;
    }

    /**
     * Add invite event
     *
     * @param string $sUser initiator
     * @param string $sInviteUser target
     * @param int $iGroupId
     * @param int $iType group category id
     */
    function addInvite($sUser, $sInviteUser, $iGroupId, $iType)
    {
        $oGroupInviteItem = $this->_oModule->getItem('GroupInvite');
        $sSql = 'INSERT INTO `'.App::getT('group_invite').'` (`type_id`, `group_id`, `user`, `invite`, `time`) VALUES
				('.$iType.','.$iGroupId.',"'.$sUser.'","'.$sInviteUser.'",'.time().')';
        $oGroupInviteItem->oDb->query($sSql);
    }


    /**
     * Delete group by id
     *
     * @param int $iGroupId
     */
    function deleteGroup($iGroupId)
    {
        $oItem = $this->_oModule->getItem('Groups');

        $oGalleryService = App::getModuleService('Groups','Gallery');
        list($iCnt, $aImages) = $oGalleryService->getImages($iGroupId);
        if($iCnt)
        {
            foreach($aImages as $iItem)
            {
                $oGalleryService->deleteItem($iItem['id']);
            }
        }

        //$oForumService = App::getModuleService('Groups','Forum');
        //$oForumService->deleteForumByGroupId($iGroupId);

        $sSql = 'DELETE FROM `'.App::getT('group_main').'` WHERE `id` = "'.$iGroupId.'"';
        $oItem->oDb->query($sSql);
        $sSql = 'DELETE FROM `'.App::getT('group_member').'` WHERE `group_id` = "'.$iGroupId.'"';
        $oItem->oDb->query($sSql);
        $sSql = 'DELETE FROM `'.App::getT('group_invite').'` WHERE `group_id` = "'.$iGroupId.'"';
        $oItem->oDb->query($sSql);
        $this->updateGroupCount();
    }

    /**
     * Create or edit category name
     *
     * @param string $sName
     * @param int $iCategoryId (zero means insert new name)
     */

    function editCategory($sName, $iCategoryId = 0)
    {
        $oItem = $this->_oModule->getItem('Groups');
        if($iCategoryId)
        {
            //update
            $sSql = 'UPDATE `'.App::getT('group_name').'` SET `name` = "'.$oItem->oDb->escape($sName).'" WHERE `id` = "'.$iCategoryId.'"';
        }
        else
        {
            //insert
            $sSql = 'INSERT INTO `'.App::getT('group_name').'` (`name`) VALUES ("'.$oItem->oDb->escape($sName).'")';
        }
        $oItem->oDb->query($sSql);
    }

    /**
     * Delete category by id
     *
     * @param int $itegoryId
     */
    function deleteCategory($iCategoryId)
    {
        $oItem = $this->_oModule->getItem('Groups');

        $sql='SELECT id FROM `'.App::getT('group_main').'` WHERE type="'.$iCategoryId.'"';
        $aCategoryGroups=$oItem->oDb->getRows($sql);
	foreach($aCategoryGroups as $aOneGroup) {
            $this->deleteGroup($aOneGroup['id']);
	}

        $sSql = 'DELETE FROM `'.App::getT('group_name').'` WHERE `id` = "'.$iCategoryId.'"';
        $oItem->oDb->query($sSql);
    }

    /**
     * Check unique category name
     *
     * @param string $sTitle
     * @param int $iGroupId
     * @return boolean
     */

    function isUniqueName($sTitle, $iGroupId)
    {
        $sTitle = trim($sTitle);
        $oItem = $this->_oModule->getItem('Groups');
        $sSql = 'SELECT COUNT(id) AS cnt FROM `'.App::getT('group_main').'` WHERE `title`="'.$oItem->oDb->escape($sTitle).'"';

        if($iGroupId)
        {
            $sSql .= ' AND `id`<>'.$iGroupId;
        }

        $aResult = $oItem->oDb->getRow($sSql);
        return $aResult['cnt'] == 0;
    }

  function getOwnGroups($sUser)
  {
      $oItem = $this->_oModule->getItem('Groups');
      $sSql = 'SELECT id, title FROM `'.App::getT('group_main').'` WHERE `user`="'.$oItem->oDb->escape($sUser).'"';
      $aResult = $oItem->oDb->getRows($sSql);
      return $aResult;
  }

  function getMemberGroups($sUser)
  {
      $oItem = $this->_oModule->getItem('Groups');
      $sSql = 'SELECT DISTINCT main.id,main.title FROM `'.App::getT('group_member').'` AS memb LEFT JOIN `'.App::getT('group_main').'` AS main ON memb.group_id=main.id WHERE memb.user="'.$oItem->oDb->escape($sUser).'"';
      $aResult = $oItem->oDb->getRows($sSql);
      return $aResult;
  }

    /**
     * Check unique category name
     *
     * @param string $sName
     * @param int $iCategoryId
     * @return boolean
     */

  function isUniqueCategory($sName, $iCategoryId)
    {
        $sName = trim($sName);
        $oItem = $this->_oModule->getItem('Groups');
        $sSql = 'SELECT COUNT(id) AS cnt FROM `'.App::getT('group_name').'` WHERE `name`="'.$oItem->oDb->escape($sName).'"';
        if($iCategoryId)
        {
            $sSql .= ' AND `id`<>'.$iCategoryId;
        }

        $aResult = $oItem->oDb->getRow($sSql);
        return $aResult['cnt'] == 0;
    }


    /**
     * Retrive used space by user within group (images)
     *
     * @param string $sUser
     * @return int size in bytes
     */
    function getUsedSpace($sUser = '')
    {
        $oGalleryItem = $this->_oModule->getItem('GroupGallery');
        /* $oGalleryItem PhpFox_Mod_Gallery_ItemGallery */
        if (!$sUser)
        {
            $oSecurityService = &App::getModuleService('Account', 'Security');
            /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
            $sUser = $oSecurityService->getCurrentUserLogin();
        }
        if (!$sUser)
        {
            return false;
        }
        $aRows = $oGalleryItem->oDb->getRows('SELECT id, user, img '.
                                            ' FROM '.App::getT('group_gallery').' '.
                                            ' WHERE user = "'.$sUser.'"');
        $iSize = 0;
        foreach ($aRows as $aRow)
        {
            $sPath = App::getParam('groups.pic.path');
            $iSize += (file_exists($sPath.$aRow['id'].'.jpg')) ? filesize($sPath.$aRow['id'].'.jpg') : 0;
            $iSize += (file_exists($sPath.'thumb/'.$aRow['id'].'.jpg')) ? filesize($sPath.'thumb/'.$aRow['id'].'.jpg') : 0;
        }
        return $iSize;
    }
    
	function getAccessForContent($sFile)
    {
    	$iItemId = intval($sFile);
        if (!$iItemId)
        {
        	return FALSE;
        }
        $oItem = $this->_oModule->getItem('GroupGallery');
        
        if ($oItem->load($iItemId))
        {
        	$iGroupId = $oItem->aData['group_id'];
	        $aGroupData = $this->getGroupData($iGroupId);//d($aGroupData,1);
	        return(($this->isMember($iGroupId, App::getUser('user')) OR App::isStaff()) OR $aGroupData['hide_group'] != 'Yes');
        }
		return FALSE;
    }
 }
?>